<template>
    <div class="pane-wrap">
        <div class="rs-toolbar">
            <div class="title">当前操作的机构：{{unitName}}</div>
            <el-button type="primary" size="small" icon="el-icon-circle-plus" @click="onAdd" :disabled="btnDisabled">新增</el-button>
        </div>
        <el-table
            ref="unitTable"
            :max-height="tableHeight"
            :height="tableHeight"
            :data="depts"
            border
            size="small"
            highlight-current-row
            style="width: 100%"
            >
                <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
                <el-table-column prop="bmbh" label="机构编号" width="100"></el-table-column>
                <el-table-column prop="bmmc" label="机构名称" width="400"></el-table-column>
                <!-- <el-table-column prop="bmpmc" label="上级机构" width="400"></el-table-column> -->
                <el-table-column prop="bmxh" label="排序号" width="80" align="right"></el-table-column>
                <el-table-column
                    fixed="right"
                    label="操作"
                    minWidth="100">
                    <template slot-scope="scope">
                        <el-button class="rs-table-btn" @click="onEdit(scope.row)" type="text" size="mini">编辑</el-button>
                        <el-button class="rs-table-btn" @click="onDelete(scope.row)" type="text" size="mini">删除</el-button>                        
                    </template>
                </el-table-column>
        </el-table>
        <RsPagination :currentPage="currentPage" :pageSize="pageSize" :totalCount="totalCount" @change="onPageInfoChange"></RsPagination>
        <UnitForm ref="unitForm"></UnitForm>
    </div>
</template>
<script>
import mixin from './mixin'
import UnitForm from './unitForm'
import page from '@/pages/mixin/page.js'
import unit from '@/api/unit'
import tips from '../../mixin/tips'
export default {
    mixins:[mixin,page,tips],
    components:{UnitForm},
    inject: ['unit'],
    props:{
        currentNode:null
    },
    data(){
        return {
            totalCount:0,
            pageSize:20,
            currentPage:0,
            depts: [],
            updateList:false
        }
    },
    watch:{
        currentNode(newValue,oldValue){

            if( this.unit.activeName ==='unit'){

                this.currentPage = 0;
                this.totalCount = 0;
                const deptData = newValue.data;
                this.loadChildList(deptData.bmbh);
            }else{
                this.updateList = true;
                this.depts = []
            }
        },
        'unit.activeName'(newValue,oldValue){

            if( this.unit.activeName ==='unit' && this.updateList){
                this.currentPage = 0;
                this.totalCount = 0;
                const deptData = this.currentNode.data;
                this.loadChildList(deptData.bmbh);
                this.updateList = false;
            }
        }
    },
    computed:{
        btnDisabled(){
            
            return this.currentNode == null || this.currentNode.data.bmbh ==='000000000'
        }
    },
    methods:{
        onPageInfoChange(obj){
            this.pageSize = obj.pageSize;
            this.currentPage = obj.currentPage;
            this.reloadDeptList();
        },
        loadChildList(bmpbh){

            unit.queryDept({
                    bmpbh:bmpbh,
                    pageSize:this.pageSize,
                    page:this.currentPage
                })
            .then(response => {
                this.loading = false;
                const { data } = response;
                //console.log(data)
                this.totalCount = data.totalCount;
                if( data.error_code == 0 ){
                    
                    this.depts = data.list;
                    this.totalCount = data.totalCount;
                    //this.$store.commit('app/setAuthInfo',data);
                }else{
                    //this.showError('获取授权信息失败！');
                }
            }).catch(error => {
                this.loading = false;
                //this.showError('网络错误，获取授权信息失败！');
            });
        },
        onAdd(){

            const parentData = this.currentNode.data;
            const unitForm = this.$refs.unitForm;
            unitForm.show({
                bmpbh:parentData.bmbh,
                bmpmc:parentData.bmmc
            })
            .then((action)=>{
                //console.log(action)
                if( action == 'ok'){
                    this.reloadDeptList();
                }
            });
        },
        onEdit(data){

            //console.log(data)
            const unitForm = this.$refs.unitForm;
            unitForm.show(data)
            .then((action)=>{
                //console.log(action)
                if( action == 'ok'){
                    this.reloadDeptList();
                }
            });
        },
        onDelete(dept){

            this.$confirm('是否删除该机构？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {

                this.deleteDept(dept);
            }).catch(() => {

                this.$message({type: 'info',message: '删除异常'});
            });
        },
        deleteDept(dept){
            
            const ids = []
            ids.push(dept.bmbh)
            unit.delDept(ids)
            .then(response => {
                const { data } = response;
                this.reloadDeptList();
                const node = this.unit.findNode(dept.bmbh);
                this.unit.deleteNodeData(node);
            }).catch(error => {
                console.log(error)
            });
        },
        reloadDeptList(){
            const deptData = this.currentNode.data;
            this.loadChildList(deptData.bmbh);
        }
    }
}
</script>